package loveqq.crud.service;

import loveqq.crud.entity.User;
import loveqq.crud.query.UserQuery;
import loveqq.crud.repository.UserRepository;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class UserService {

    @Autowired
    UserRepository userRepository;

    public Flux<User> findAll() {
        return userRepository.findAll();
    }

    public Flux<User> search(Mono<UserQuery> query) {
        return userRepository.findAll();
    }

    @Transactional
    public Mono<Void> save(Publisher<User> user) {
        return userRepository.saveAll(user).then();
    }

    @Transactional
    public Mono<Void> remove(Publisher<Long> id) {
        return userRepository.deleteById(id);
    }

    @Transactional
    public Mono<Void> update(Publisher<User> user) {
        return userRepository.saveAll(user).then();
    }
}
